package cn.sumpu.app.guoguojie.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

public class HttpProxy {

	/**
	 * 通过HTTP协议把图片数据发送到指定的图片服务器上
	 * 
	 * @param serverUrl
	 *            图片服务器地址
	 * @param savePath
	 *            图片在图片服务器上的保存地址
	 * @param saveFilename
	 *            图片在图片服务器上的保存文件名
	 * @param imageData
	 *            图片数据
	 * @return ok-成功；error-失败
	 * @throws Exception
	 *             HTTP交互过程中产生异常
	 */
	public static String sendImage(String serverUrl, String savePath,
			String saveFilename, byte[] imageData) throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpPost httppost = new HttpPost(serverUrl);

		MultipartEntity multipartEntity = new MultipartEntity();
		multipartEntity.addPart("savePath",
				new StringBody(savePath, Charset.forName("UTF-8")));
		multipartEntity.addPart("saveFilename", new StringBody(saveFilename,
				Charset.forName("UTF-8")));
		multipartEntity.addPart("pic",
				new ByteArrayBody(imageData, "image.jpg"));
		httppost.setEntity(multipartEntity);

		HttpResponse response = httpclient.execute(httppost);
		HttpEntity resEntity = response.getEntity();

		String ret = null;
		if (resEntity != null) {
			String charset = EntityUtils.getContentCharSet(resEntity);
			ret = EntityUtils.toString(resEntity, charset);
		}

		httpclient.getConnectionManager().shutdown();

		return ret;
	}

	/**
	 * 通过HTTP协议删除指定的图片服务器上的图片文件
	 * 
	 * @param serverUrl
	 *            图片服务器地址
	 * @param imageFile
	 *            要删除的图片在图片服务器上的保存文件名
	 * @return ok-成功；error-失败
	 * @throws Exception
	 *             HTTP交互过程中产生异常
	 */
	public static String deleteImage(String serverUrl, String imageFile)
			throws Exception {
		HttpClient httpclient = new DefaultHttpClient();
		httpclient.getParams().setParameter(
				CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		HttpPost httppost = new HttpPost(serverUrl);

		MultipartEntity multipartEntity = new MultipartEntity();
		multipartEntity.addPart("imageFile",
				new StringBody(imageFile, Charset.forName("UTF-8")));
		httppost.setEntity(multipartEntity);

		HttpResponse response = httpclient.execute(httppost);
		HttpEntity resEntity = response.getEntity();

		String ret = null;
		if (resEntity != null) {
			String charset = EntityUtils.getContentCharSet(resEntity);
			ret = EntityUtils.toString(resEntity, charset);
		}

		httpclient.getConnectionManager().shutdown();

		return ret;
	}

	// just test
	public static void main(String[] args) throws Exception {
		String serverUrl = "http://192.168.0.105:8080/receiveImage.ajax";
		String savePath = "tmp/pic/20110621";
		String saveFilename = "7d905506-6595-4949-b06a-cda0c1c57231.jpg";
		String fileName = "/home/nick/work/guoguojie/guoguojie-common/src/test/resources/WaterLilies.jpg";
		InputStream fis = new FileInputStream(fileName);
		byte[] b = new byte[fis.available()];
		fis.read(b);
		System.err.println(sendImage(serverUrl, savePath, saveFilename, b));

		serverUrl = "http://192.168.0.105:8080/deleteImage.ajax";
		String imageFile = "tmp/pic/20110627/1324327428/76c31059-82cc-4141-a932-97e1f390fe02.jpg";
		System.err.println(deleteImage(serverUrl, imageFile));

	}

}
